//获取应用实例
var app = getApp();
var util = require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    load1:'装货地',//发货地
    load2:'卸货地',//收货地
    model:'车型',//车型
    length:'载重',//载重
    show:false,
    loading: true,
    page: 1,//当前页码
    totalpage:100,//总页码
    lock: false,
    pallet: [],//定义货盘列表
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBasicInfo();
    this.getData();
  },
// 获取货盘筛选基础信息：发货地、送货地、车型
  getBasicInfo: function(res){
    var that = this;
    app.getLoginSession(function (session) {
      // 发货地
      wx.request({
        url: app.api + '/wx_log_test/sys/departure',
        data: {
          token: session
        },
        success: function (res) {
          if(res.statusCode == 200){
            if(res.data.msg == 'OK'){
              that.setData({
                departure: res.data.data
              })
            }
          }
        }
      });
      // 送货地
      wx.request({
        url: app.api + '/wx_log_test/sys/destination',
        data: {
          token: session
        },
        success: function (res) {
          if (res.statusCode == 200) {
            if (res.data.msg == 'OK') {
              that.setData({
                destination: res.data.data
              })
            }
          }
        }
      });
      // 车型
      wx.request({
        url: app.api + '/wx_log_test/sys/vehicle',
        data: {
          token: session
        },
        success: function (res) {
          if (res.statusCode == 200) {
            if (res.data.msg == 'OK') {
              that.setData({
                vehicle: res.data.data
              })
            }
          }
        }
      })
    })
  },

  //获得货盘
  getData: function () {
    var that = this;
    if(that.data.page<=that.data.totalpage){
      that.setData({
        loadTips:'',
        errorTips:'',
        loading:true
      })
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/cargo/list?' + 'token=' + session,
          method: 'POST',
          header: {
            'content-type': 'application/json' // 默认值
          },
          data: {
            page: that.data.page,
            per_page: 10,
            arrival_id: that.data.load2 == '卸货地' ? '' : that.data.load2,
            company_id: "",
            departure_id: that.data.load1 == '装货地' ? '' : that.data.load1,
            status: 1,
            truck_load: that.data.length == '载重' ? 0 : that.data.length,
            truck_type: that.data.model == '车型' ? '' : that.data.model,
          },
          success: function (e) {
            console.log(e);
            var res = e.data;
            if (res.msg == "OK") {
              if (res.data.totalPages == 0){
                that.setData({
                  loadTips: "暂无数据"
                })
              } 
              that.setData({
                pallet: that.data.pallet.concat(that.deallist(res.data.content)),
                loading: false,
                page: ++that.data.page
              })
              if (that.data.page-1 == res.data.totalPages){
                that.setData({
                  loadTips: "暂无更多数据"
                })
              }
            } else {
              that.setData({
                loading: true,
                errorTips: '接口请求失败' + e.statusCode
              })
            }
          },
          fail: function (e) {
            that.setData({
              loading: true,
              errorTips: '接口请求失败' + e.statusCode
            })
          }
        })
      })
    }
    
  },
  // 上拉触底
  lower: function () {
    var that = this;
    that.getData();
  },
  call: function (res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.num //仅为示例，并非真实的电话号码
    })
  },
  //载重
  load: function (res) {
    this.setData({
      length: res.detail.value,
      show: this.data.show ? false : true
    })
    this.setData({
      ids: '',
      page: 1,
      totalpage: 1,
      pallet: []
    })
    this.getData();
  },
  // 发货地
  load1: function (e) {
    var that = this;
    that.setData({
      show:that.data.show?false:true
    })
    console.log(e);
    if(that.data.ids==1){
      that.setData({
        load1: e.currentTarget.dataset.name,
        ids:'',
        load1num: e.currentTarget.dataset.num,
        page:1,
        totalpage:1,
        pallet:[]
      })
      that.getData();
    }else if(that.data.ids==2){
      that.setData({
        load2: e.currentTarget.dataset.name,
        ids: '',
        load2num: e.currentTarget.dataset.num,
        page: 1,
        totalpage: 1,
        pallet: []
      })
      that.getData();
    }
    else if (that.data.ids == 3) {
      that.setData({
        model: e.currentTarget.dataset.name,
        ids: '',
        page: 1,
        totalpage: 1,
        pallet: []
      })
      that.getData();
    }
  },
  // 点击下拉
  show:function(e){
    var that = this;
    that.setData({
      show: true,
      ids:e.currentTarget.dataset.id
    })
    if(e.currentTarget.dataset.id==1){
      that.setData({
        showdata: that.data.departure
      })
    } else if (e.currentTarget.dataset.id == 2) {
      that.setData({
        showdata: that.data.destination
      })
    } else if (e.currentTarget.dataset.id == 3) {
      that.setData({
        showdata: that.data.vehicle
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  deallist(list) {
    if(list && Array.isArray(list)) {
      list.forEach((item) => {
        console.log(item);
        item.send_date1 = util.formatDate(new Date(item.send_date1), 'YYYY-MM-dd')
      })
      return list
    }
  }
})